package com.hulk.dryad.manage.framework.websocket.server.error;

import com.hulk.dryad.common.component.R;
import com.hulk.dryad.common.exception.BaseRteException;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.messaging.MessagingException;
import org.springframework.messaging.handler.annotation.MessageExceptionHandler;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.web.bind.annotation.ControllerAdvice;

/**
 * websocket 异常处理
 *
 * @author
 */
@Slf4j
@ControllerAdvice
@RequiredArgsConstructor
public class WebSocketErrorHandler {


	@MessageExceptionHandler(MessagingException.class)
	@SendToUser("${websocket.server.queue.errors}")
	public R<Object> handleExceptions(MessagingException e) {
		log.error(e.getMessage(), e);
		return R.failed("消息传递中发生异常");
	}
	@MessageExceptionHandler(BaseRteException.class)
	@SendToUser("${websocket.server.queue.errors}")
	public R<Object> handleExceptions(BaseRteException e) {
			log.error(e.getMessage(), e);
			return R.failed("websocket 未知异常");
	}
	@MessageExceptionHandler(Throwable.class)
	@SendToUser("${websocket.server.queue.errors}")
	public R<Object> handleExceptions(Throwable e) {
		if (e != null) {
			log.error(e.getMessage(), e);
		}
		return R.failed("websocket未知异常");
	}

}
